home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / Demos_Files / Code_Generation / xorConsoleTesting.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  3.6 KB  |  125 lines

  1. // xorConsoleTesting.cpp
  2. // Automatically generated from breadboard by NeuroSolutions.
  3.  
  4. #include "NSLib.h"
  5.  
  6. void inputFileAccess(NSFloat *, int, int);
  7. void activeOutputProbeAccess(NSFloat *, int, int);
  8. BOOL networkStopped=FALSE;
  9. unsigned short int weightFileVersion=225;
  10.  
  11. // Component Construction
  12. Axon inputAxon;
  13. FunctionIO inputFile;
  14. FullSynapse hidden1Synapse;
  15. TanhAxon hidden1Axon;
  16. FullSynapse outputSynapse;
  17. TanhAxon outputAxon;
  18. FunctionIO activeOutputProbe;
  19.  
  20. int main() {
  21.     srand((unsigned)time(NULL));
  22.  
  23.     // Component Initialization
  24.     inputAxon.setRows(2);
  25.     inputFile.setFunction((void*)inputFileAccess);
  26.     inputFile.setMode(READ);
  27.     inputFile.setSpatialDimension(2,1);
  28.     hidden1Axon.setRows(2);
  29.     outputAxon.setRows(1);
  30.     activeOutputProbe.setFunction((void*)activeOutputProbeAccess);
  31.     activeOutputProbe.setMode(WRITE);
  32.     activeOutputProbe.setSpatialDimension(1,1);
  33.     FILE *loadStream = fopen("xorConsoleTesting.nsw","r");
  34.     if (!loadStream) {
  35.         fprintf(stderr, "Could not open weight file xorConsoleTesting.nsw");
  36.         exit(1);
  37.     }
  38.     weightFileVersion = getWeightFileVersion(loadStream);
  39.  
  40.     // Load Normalization Coefficients of Files
  41.     inputFile.loadWeights(seekComponent(loadStream, "File", "inputFile"),weightFileVersion);
  42.  
  43.     // Component Interconnection
  44.     inputAxon.setPreActivityAccess(&inputFile);
  45.     outputAxon.setActivityAccess(&activeOutputProbe);
  46.     inputAxon.setNext(&hidden1Synapse);
  47.     hidden1Synapse.setLast(&inputAxon);
  48.     hidden1Synapse.setNext(&hidden1Axon);
  49.     hidden1Axon.setLast(&hidden1Synapse);
  50.     hidden1Axon.setNext(&outputSynapse);
  51.     outputSynapse.setLast(&hidden1Axon);
  52.     outputSynapse.setNext(&outputAxon);
  53.     outputAxon.setLast(&outputSynapse);
  54.  
  55.     // Load Axon Weights
  56.     inputAxon.loadWeights(seekComponent(loadStream, "Axon", "inputAxon"),weightFileVersion);
  57.     hidden1Axon.loadWeights(seekComponent(loadStream, "TanhAxon", "hidden1Axon"),weightFileVersion);
  58.     outputAxon.loadWeights(seekComponent(loadStream, "TanhAxon", "outputAxon"),weightFileVersion);
  59.  
  60.     // Load Synapse Weights
  61.     hidden1Synapse.loadWeights(seekComponent(loadStream, "FullSynapse", "hidden1Synapse"),weightFileVersion);
  62.     outputSynapse.loadWeights(seekComponent(loadStream, "FullSynapse", "outputSynapse"),weightFileVersion);
  63.     fclose(loadStream);
  64.  
  65.     // Get Ready to Run Network
  66.  
  67.     // Run Network
  68.     int epochs = 110;
  69.     int exemplars = 4;
  70.     int epochsPerCV = 0;
  71.     for (int epoch=0; epoch<epochs; epoch++) {
  72.         for (int exemplar=0; exemplar<exemplars; exemplar++) {
  73.             inputAxon.fire();
  74.             if (networkStopped) {
  75.                 goto ConcludeFiring;
  76.             }
  77.         }
  78.         if (networkStopped)
  79.             goto ConcludeFiring;
  80.     }
  81.  
  82.     ConcludeFiring:
  83.  
  84.  
  85.     return 0;
  86. }
  87.  
  88. void inputFileAccess(NSFloat *data, int rows, int cols) {
  89.     // This function is called each time the inputAxon's Pre-Activity data changes
  90.     // This function's code was written after NeuroSolutions generated the code for the BB
  91.     // (Begin)
  92.     int     length,
  93.         i;
  94.     char    response[128];
  95.  
  96.     length = rows*cols;
  97.     printf("Enter the space-delimited data samples ('exit' to quit)):\n");
  98.     scanf("%s",response);
  99.     if (strcmp(response,"exit") != 0) {
  100.         sscanf(response,"%f", data);
  101.         for (i=1; i<length; i++)
  102.             scanf("%f", data+i);
  103.     }
  104.     else
  105.         networkStopped=TRUE;
  106.     // (End)
  107. }
  108.  
  109. void activeOutputProbeAccess(NSFloat *data, int rows, int cols) {
  110.     // This function is called each time the outputAxon's Activity data should be changed
  111.     // This function's code was written after NeuroSolutions generated the code for the BB
  112.     // (Begin)
  113.     int     j,
  114.         i;
  115.  
  116.     printf("Network output:\n");
  117.     for (i=0; i<rows; i++) {
  118.         for (j=0; j<cols; j++)
  119.             printf("%f ", data[i*cols + j]);
  120.         printf("\n");
  121.     }
  122.     // (End)
  123. }
  124.  
  125.